package com.example.recyclerviewdemo.recyclerView;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.recyclerviewdemo.R;
import com.example.recyclerviewdemo.listview.Fruit;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wdl
 */
public class RecyclerViewDemo extends AppCompatActivity {
    private RecyclerView mRecyclerView;

    private List<Fruit> fruitList = new ArrayList<>();

    private String[] data = {
            "Apple", "Banana", "Orange", "WaterMelon", "Pear",
            "Apple1", "Banana1", "Orange1", "WaterMelon1", "Pear1",
            "Apple2", "Banana2", "Orange2", "WaterMelon2", "Pear2",
            "Apple3", "Banana3", "Orange3", "WaterMelon3", "Pear3",
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view_demo);

        mRecyclerView = findViewById(R.id.recycler_view);
        //初始化水果数据
        initFruitList();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        FruitAdapter fruitAdapter = new FruitAdapter(fruitList);
        mRecyclerView.setAdapter(fruitAdapter);
    }

    /**
     * 初始化fruit的数据
     */
    private void initFruitList() {
        for (int i = 0; i < data.length; i++) {
            Fruit fruit = new Fruit(data[i], R.drawable.ic_launcher_foreground);
            fruitList.add(fruit);
        }
    }
}
